
if CLIENT then

BMPedit = {};
BMPedit.bmp = {};
BMPedit.color = Color(255,255,255,255);
BMPedit.ondraw = false;
BMPedit.mode = 0;
BMPedit.FillLimit=10000
BMPedit.FillShape = true


BMPedit.GetColor= function() 
    return Color(255,255,255,255)
    end
BMPedit.GetThick= function() 
    return 1
    end
	

function BMPedit:SetEditorColor(col)
	
    end


-------------------------------------------------------
--Name: BMPedit:Init()
--Initialization
-------------------------------------------------------

function BMPedit:Init()
    self.DSliderY = vgui.Create( "DSlider", self )
		self.DSliderY:SetPos( 520, 24 )
		self.DSliderY:SetSize( 15, 464 )
		self.DSliderY:SetLockX(0.5)
		self.DSliderY:SetSlideY(0)
		self.DSliderY:SetImage( "vgui/slider" )
		self.DSliderY:SetTrapInside( true )
	self.DSliderX = vgui.Create( "DSlider", self )
		self.DSliderX:SetPos( 24, 520 )
		self.DSliderX:SetSize( 464, 15 )
		self.DSliderX:SetLockY(0.5)
		self.DSliderX:SetSlideX(0)
		self.DSliderX:SetImage( "vgui/slider" )
		self.DSliderX:SetTrapInside( true )
	
	self.bmp=LocalPlayer().BMPTemp
	
	if not self.bmp then self:Clear(32,32) end
	
end

-------------------------------------------------------
--Name: BMPedit:Clear()
--Clear and Change size
-------------------------------------------------------

function BMPedit:Clear(X, Y, Flag)
    if not X or not Y then return end
    self.bmp={}
	self.bmp.Xsize=X
	self.bmp.Ysize=Y
	local color
	if Flag then
		color = Color(0,0,0,0)
	else
		color = Color(255,255,255,255)
	end
    for x=1,X do 
        self.bmp[x]={}
        for y=1,Y do 
            self.bmp[x][y]=color
        end 
    end 
end

-------------------------------------------------------
--Name: BMPedit:Paint()
--Paint
-------------------------------------------------------

function BMPedit:Paint()
    
	local offsetX=math.Round(self.DSliderX:GetSlideX()*448)
	local offsetY=math.Round(self.DSliderY:GetSlideY()*448)
	
	surface.SetFont("TabLarge")
	
	surface.SetDrawColor(127,127,127,255)
    surface.DrawRect(0,0,512,512)
	surface.SetDrawColor(255,255,255,255)
    surface.SetTexture(surface.GetTextureID("BMPEditor_bg"))
    surface.DrawTexturedRectUV(0,0,math.min(64,self.bmp.Xsize-offsetX)*8,math.min(64,self.bmp.Ysize-offsetY)*8,7,7)
    local x2
	local y2
    for x=1,math.min(64,self.bmp.Xsize-offsetX) do 
		x2=x+offsetX
        for y=1,math.min(64,self.bmp.Ysize-offsetY)  do 
			y2=y+offsetY
            surface.SetDrawColor( self.bmp[x2][y2] )
            surface.DrawRect( (x-1)*8,(y-1)*8,8,8)
            if x==math.min(64,self.bmp.Xsize-offsetX) then
                if (y2-1)%32==0 then
                    surface.SetDrawColor(255,255,255,255)
					surface.SetTextPos(2,(y-1)*8)
					surface.DrawText((y2-1))
					surface.SetDrawColor(127,127,127,250)
                elseif (y2-1)%8==0 then
                    surface.SetDrawColor(255,255,255,255)
					surface.SetTextPos(2,(y-1)*8)
					surface.DrawText((y2-1))
                    surface.SetDrawColor(127,127,127,200)
                elseif (y2-1)%4==0 then
                    surface.SetDrawColor(127,127,127,150)
                else
                    surface.SetDrawColor(127,127,127,100)
                end
                surface.DrawLine( 0,(y-1)*8,math.min(64,self.bmp.Xsize-offsetX)*8,(y-1)*8)
            end
        end 
        if (x2-1)%32==0 then
            surface.SetDrawColor(127,127,127,250)
        elseif (x2-1)%8==0 then
            surface.SetDrawColor(127,127,127,200)
        elseif (x2-1)%4==0 then
            surface.SetDrawColor(127,127,127,150)
        else
            surface.SetDrawColor(127,127,127,100)
        end
        surface.DrawLine( (x-1)*8,0,(x-1)*8,math.min(64,self.bmp.Ysize-offsetY)*8)
		if (x2-2)%32==0 then
            surface.SetDrawColor(255,255,255,255)
			surface.SetTextPos((x-3)*8,0)
			surface.DrawText(x2-2)
        elseif (x2-2)%8==0 then
            surface.SetDrawColor(255,255,255,255)
			surface.SetTextPos((x-3)*8,0)
			surface.DrawText(x2-2)
		end
    end 

    surface.SetDrawColor(0,0,0,255)
    surface.DrawOutlinedRect(0,0,512,512)
	
	draw.RoundedBox( 6, 522, 25, 12, 463, Color(127,127,127,255) )
	surface.SetDrawColor(50,50,50,255)
	surface.DrawLine(528, 31, 528, 482 )
	surface.SetDrawColor(30,30,30,255)
	surface.DrawLine(527, 31, 527, 482 )
	
	draw.RoundedBox( 6, 25, 522, 463, 12, Color(127,127,127,255) )
	surface.SetDrawColor(50,50,50,255)
	surface.DrawLine(31, 528, 482, 528 )
	surface.SetDrawColor(30,30,30,255)
	surface.DrawLine(31, 527, 482, 527 )
	
	if self.ondraw then
		local X, Y = self:CursorPos()
		local X2 = (self.lastx-offsetX)*8-4
		local Y2 = (self.lasty-offsetY)*8-4
		
		X=math.floor(X/8)*8+4
		Y=math.floor(Y/8)*8+4
		
		
		
		local col = table.Copy(self.color)
		
		if self.mode == 4 then
		
			col.a=255
			surface.SetDrawColor(col)
			
			surface.DrawLine( X, Y, X2, Y2)
			
		elseif self.mode == 5 then
		
			col.a=127
			surface.SetDrawColor(col)
			
			if X>X2 then 
				local temp = X2
				X2=X
				X=temp
			end
			
			if Y>Y2 then 
				local temp = Y2
				Y2=Y
				Y=temp
			end
			
			surface.DrawRect( X, Y, X2-X, Y2-Y)
			
		elseif self.mode == 6 then
		
		end
		
	end
	
    return true;
end

-------------------------------------------------------
--Name: BMPedit:doLine()
--Draw Line
-------------------------------------------------------

function BMPedit:doLine(x1, y1, x2, y2)
	if x1 == nil or y1 == nil then
		x1=x2
		y1=y2
	end
	
	if x2 == nil or y2 == nil then
		x2=x1
		y2=y1
	end
	
	if x2 == nil or y2 == nil then return end
	
	local d, dx, dy, ai, bi, xi, yi;
	local x = x1; 
	local y = y1;
	if x1 < x2 then
		xi = 1
		dx = x2 - x1;
	else
		xi = -1;
		dx = x1 - x2;
	end
	if y1 < y2 then
		yi = 1;
		dy = y2 - y1;
	else
		yi = -1;
		dy = y1 - y2;
	end
	if x > self.bmp.Xsize or y > self.bmp.Ysize or x<=0 or y<=0 then return end
	self.bmp[x][y]=table.Copy(self.color)
	if dx > dy then
		ai = (dy - dx) * 2;
		bi = dy * 2;
		d = bi - dx;
		while x != x2 do
			if d >= 0 then
				x = x + xi;
				y = y + yi;
				d = d + ai;
			else
				d = d + bi;
				x = x + xi;
			end
			if x > self.bmp.Xsize or y > self.bmp.Ysize or x<=0 or y<=0 then return end
			self.bmp[x][y]=table.Copy(self.color)
			end 
		else
		ai = ( dx - dy ) * 2;
		bi = dx * 2;
		d = bi - dy;
		while (y != y2) do
			if (d >= 0) then
				x = x + xi;
				y = y + yi;
				d = d + ai;
			else
				d = d + bi;
				y = y + yi;
			end
		if x > self.bmp.Xsize or y > self.bmp.Ysize or x<=0 or y<=0 then return end
		self.bmp[x][y]=table.Copy(self.color)
		end
	end
end

-------------------------------------------------------
--Name: BMPedit:fill()
--fill area
-------------------------------------------------------

function BMPedit:fill(X, Y, From, To)
	if X>self.bmp.Xsize or Y>self.bmp.Ysize or X<=0 or Y<=0 then return end
	if self.bmp[X][Y].a != 0 or From.a != 0 then
		if self.bmp[X][Y].r != From.r then return end
		if self.bmp[X][Y].g != From.g then return end
		if self.bmp[X][Y].b != From.b then return end
		if self.bmp[X][Y].a != From.a then return end
	end
	
	
	self.FillCount = self.FillCount + 1
	self.bmp[X][Y] = table.Copy(To)
	
	if self.FillCount > self.FillLimit then return end
	self:fill(X+1, Y, From, To)
	if self.FillCount > self.FillLimit then return end
	self:fill(X, Y+1, From, To)
	if self.FillCount > self.FillLimit then return end
	self:fill(X-1, Y, From, To)
	if self.FillCount > self.FillLimit then return end
	self:fill(X, Y-1, From, To)
end

-------------------------------------------------------
--Name: BMPedit:OnMousePressed()
--Start Drawing
-------------------------------------------------------

function BMPedit:OnMousePressed( MC )
    
	if (math.max(self:CursorPos())<512) then
		
		local X, Y = self:CursorPos()
		local offsetX=math.Round(self.DSliderX:GetSlideX()*448)
		local offsetY=math.Round(self.DSliderY:GetSlideY()*448)
		X=math.floor(X/8)+1+offsetX
		Y=math.floor(Y/8)+1+offsetY

		
		if X>self.bmp.Xsize or Y>self.bmp.Ysize then return end
		
		if self.mode == 0 then
			
			self.color=self.GetColor()
			
			self.ondraw = true
		elseif self.mode == 1 then
			
			print("Fill in: " .. X ..", " .. Y)
			PrintTable(self.bmp[X][Y])
			PrintTable(self.GetColor())
			
			self.color = self.GetColor()
			
			if self.bmp[X][Y].a == 0 and self.color.a == 0 then return end
			if self.bmp[X][Y].r == self.color.r and self.bmp[X][Y].g == self.color.g and self.bmp[X][Y].b == self.color.b and self.bmp[X][Y].a == self.color.a then return end
			
			self.FillCount=0
			
			self:fill(X, Y, self.bmp[X][Y], self.color)
			
		elseif self.mode == 2 then

			
		elseif self.mode == 3 then
			PrintTable(self.bmp[X][Y])
			self:SetEditorColor(self.bmp[X][Y])
		elseif self.mode == 4 then
			
			self.color=self.GetColor()
			
			self.lastx=X
			self.lasty=Y
			
			self.ondraw = true
		elseif self.mode == 5 then
			
			self.color=self.GetColor()
			
			self.lastx=X
			self.lasty=Y
			
			self.ondraw = true
		elseif self.mode == 6 then
			
			self.color=self.GetColor()
			
			self.lastx=X
			self.lasty=Y
			
			self.ondraw = true
		
		
		end
    end
	
	
    
    self:OnCursorMoved(self:CursorPos())
end

-------------------------------------------------------
--Name: BMPedit:OnCursorMoved()
--Drawing
-------------------------------------------------------

function BMPedit:OnCursorMoved(X, Y)
    
	local offsetX=math.Round(self.DSliderX:GetSlideX()*448)
	local offsetY=math.Round(self.DSliderY:GetSlideY()*448)
	
	if math.floor(X/8)+1+offsetX>self.bmp.Xsize or math.floor(Y/8)+1+offsetY>self.bmp.Ysize then return end
	
	if X<512 and Y<512 then
		if self.mode == 0 then
			if self.ondraw then
				local x2=math.floor(X/8)+1+offsetX
				local y2=math.floor(Y/8)+1+offsetY
				local x1=self.lastx
				local y1=self.lasty
				
				self:doLine(x2, y2, x1, y1)
				
				self.lastx=x2
				self.lasty=y2
			end
		end
	end
end

-------------------------------------------------------
--Name: BMPedit:OnMouseReleased()
--Finish Drawing
-------------------------------------------------------

function BMPedit:OnMouseReleased( MC )

	local X, Y = self:CursorPos()
	local offsetX = math.Round(self.DSliderX:GetSlideX()*448)
	local offsetY = math.Round(self.DSliderY:GetSlideY()*448)
	
	if (not self.lastx) or (not self.lasty) then return end
	
	local x1=math.floor(X/8)+1+offsetX
	local y1=math.floor(Y/8)+1+offsetY
	local x2=self.lastx+offsetX
	local y2=self.lasty+offsetY
	
	if x1>self.bmp.Xsize then
		x1=self.bmp.Xsize
	end
	if y1>self.bmp.Ysize then 
		y1=self.bmp.Ysize
	end
	
	if self.ondraw then
		if self.mode == 4 then
			self:doLine(x1,y1,x2,y2)
		elseif self.mode == 5 then
		
			if x1>x2 then
				local temp=x1
				x1=x2
				x2=temp
			end
			if y1>y2 then
				local temp=y1
				y1=y2
				y2=temp
			end
		
			if self.FillShape then
				for Ix=x1,x2 do
					for Iy=y1,y2 do
						self.bmp[Ix][Iy]=table.Copy(self.color)
					end
				end
			else
				for I=x1,x2 do
					self.bmp[I][y1]=table.Copy(self.color)
				end
				for I=x1,x2 do
					self.bmp[I][y2]=table.Copy(self.color)
				end
				for I=y1+1,y2-1 do
					self.bmp[x1][I]=table.Copy(self.color)
				end
				for I=y1+1,y2-1 do
					self.bmp[x2][I]=table.Copy(self.color)
				end
			end
		end 
	end
	
    self.lastx=nil
    self.lasty=nil
	self.ondraw = false
end

vgui.Register( "BMPedit", BMPedit, "DButton");

end